package cn.bluemobi.dylan.step.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.util.Log;
import android.widget.TextView;

import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.MarkerView;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.formatter.IAxisValueFormatter;
import com.github.mikephil.charting.highlight.Highlight;
import com.github.mikephil.charting.utils.MPPointF;

import java.util.ArrayList;
import java.util.List;

import cn.bluemobi.dylan.step.R;
import cn.bluemobi.dylan.step.step.bean.Weight;
import cn.bluemobi.dylan.step.step.utils.DbUtils;

public class LineChartMarkView extends MarkerView {

    private TextView tvValue;
    private TextView tvDate;
    private  List<Weight> weightList;
    private LineChart chart;
    private Context context;
    public LineChartMarkView(Context context,List<Weight> weightList,LineChart lineChart) {
        super(context, R.layout.chart_marker_view);
        tvValue = findViewById(R.id.tv_value);
        tvDate = findViewById(R.id.tv_date);
        this.weightList = weightList;
        this.chart = lineChart;
        this.context = context;
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void refreshContent(Entry e, Highlight highlight) {
        super.refreshContent(e, highlight);
        tvValue.setText(e.getY()+"Kg");
        int i = (int)e.getX();
        Log.v("date",weightList.get(i).getDate());
        tvDate.setText(weightList.get(weightList.size()-i-1).getDate());
    }

    @Override
    public MPPointF getOffset() {
        return new MPPointF((getWidth() / 8), -getHeight());
    }
    //这里修改markview偏移
    public void draw(Canvas canvas, float posX, float posY) {
        MPPointF mpPointF = new MPPointF();
        //根据项目需求通过width、height、chartWidth、chartHeight来设置point偏移位置
        float width = getWidth();//markview宽度
        float height = getHeight();//markview的高度
        float chartWidth = chart.getWidth();//chart宽度
        float chartHeight = chart.getHeight();//chart的高度
        //右边超出的情况，显示一个，隐藏一个，设置对应的偏移量
        if (chartWidth < posX + width) {
            mpPointF.x =  - (dip2px(context,10f) + getWidth());
        } else {//右边没有超出的情况，同上
            mpPointF.x = dip2px(context,10f);
        }
        //绘制方法是直接复制过来的，没动
        int saveId = canvas.save();
        // translate to the correct position and draw
        canvas.translate(posX + mpPointF.x, posY + mpPointF.y);
        draw(canvas);
        canvas.restoreToCount(saveId);
    }

    public int dip2px(Context context, float dipValue){
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int)(dipValue * scale + 0.5f);
    }
}
